﻿using System;

namespace CustomerContactSystem.WebForms.Admin.Departments {
    public partial class Edit : BasePage {
        protected void Page_Load(object sender, EventArgs e) {
            if (!IsValidPageRequest(true, true, "id")) {
                return;
            }

            GetDepartment();
        }

        private void GetDepartment() {
            var departmentId = int.Parse(Request.QueryString["id"]);
            var department = DepartmentRepository.GetById(departmentId);

            if (department == null) {
                return;
            }

            DepartmentName.Text = department.DepartmentName;
        }

        protected void SaveDepartmentClick(object sender, EventArgs e) {
            if (!IsValidPageRequest(true, false, "id")) {
                return;
            }

            var departmentId = int.Parse(Request.QueryString["id"]);
            var department = DepartmentRepository.GetById(departmentId);

            if (department == null) {
                SaveDepartmentResultMessage.Text = "Department not found!";
                return;
            }

            department.DepartmentName = DepartmentName.Text;
            DepartmentRepository.Update(department);
            Response.Redirect("~/Admin/Departments/List.aspx");
        }
    }
}